package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.annotation.RequestInfo;
import org.example.service.impl.AuthServiceImpl;
import org.example.util.Result;
import org.example.vo.RefreshVo;
import org.example.vo.TenantVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.example.enums.ResultCode.TOKEN_GENERATED_ERROR;

@RestController
@RequestMapping("openapi/v1.0/tenant")
@CrossOrigin
@Slf4j
public class AuthController {
    @Autowired
    private AuthServiceImpl authServiceImpl;
    @PostMapping("/login")
    @ResponseBody  //返回json
    @RequestInfo
    public Result getToken(@RequestBody TenantVo tenantVo){
        log.info("getToken...{}",tenantVo);
//        System.out.println(tenantVo);
        if(tenantVo.getTenantId()==null||tenantVo.getTenantSecret()==null){
            return new Result(TOKEN_GENERATED_ERROR.code,false,null,"token生成失败");
        }
        return authServiceImpl.getToken(tenantVo.getTenantId(),tenantVo.getTenantSecret());
    }
    @PostMapping("/refreshToken")
    @ResponseBody
    public Result getTokenRefresh(@RequestBody RefreshVo refreshVo){
        log.info("refreshToken...{}",refreshVo);
//        System.out.println(tenantVo);
        if(refreshVo.getRefreshToken()==null){
            return new Result(TOKEN_GENERATED_ERROR.code,false,null,"token生成失败");
        }
        return authServiceImpl.getTokenRefresh(refreshVo.getRefreshToken());
    }
}
